package com.hyjoy.web.servlet;

import com.hyjoy.entity.Order;
import com.hyjoy.entity.OrderItem;
import com.hyjoy.service.OrderService;
import com.hyjoy.service.impl.OrderServiceImpl;
import com.hyjoy.util.JsonUtil;
import net.sf.json.JSONArray;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by hyjoy on 2018/4/6.
 */
public class AdminOrderServlet extends BaseServlet {

    public String findAllByState(HttpServletRequest request, HttpServletResponse response) {
        try {
            String state = request.getParameter("state");
            OrderService orderService = new OrderServiceImpl();
            List<Order> orders = orderService.findAllByState(state);
            request.setAttribute("list", orders);
        } catch (Exception e) {

        }
        return "/admin/order/list.jsp";
    }

    public String showDetail(HttpServletRequest request, HttpServletResponse response) {
        // 设置编码
        response.setContentType("text/html; charset=utf-8");
        String oid = request.getParameter("oid");
        OrderService orderService = new OrderServiceImpl();
        try {
            Order order = orderService.getById(oid);
            if (order != null) {
                List<OrderItem> orderItems = order.getItems();
                if (orderItems != null && orderItems.size() > 0) {
                    response.getWriter().println(JsonUtil.object2json(orderItems));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
